import pickle
import numpy as np
from collections import defaultdict

# 文件路径
result_path = '/media/ross/8TB/project/lsh/deep_learning/DiffusionDet_mmdet/DiffusionDet/SOTA/word_dirs/retinanet_r50_fpn_1x_coco/result.pkl'

# 加载pickle文件
with open(result_path, 'rb') as f:
    results = pickle.load(f)

# 设置置信度阈值
confidence_thresholds = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]

# 统计每个类别在不同置信度阈值下的检测数量
class_threshold_counts = defaultdict(lambda: defaultdict(int))

# 遍历所有结果
for result in results:
    pred_instances = result['pred_instances']
    labels = pred_instances['labels'].cpu().numpy()
    scores = pred_instances['scores'].cpu().numpy()

    for label, score in zip(labels, scores):
        label = int(label)
        score = float(score)

        for threshold in confidence_thresholds:
            if score >= threshold:
                class_threshold_counts[label][threshold] += 1

# 打印每个类别在不同置信度阈值下的检测数量
print("类别ID | 总数 | >=0.1 | >=0.2 | >=0.3 | >=0.4 | >=0.5 | >=0.6 | >=0.7 | >=0.8 | >=0.9")
print("-" * 100)

for class_id in sorted(class_threshold_counts.keys()):
    total_count = class_threshold_counts[class_id][confidence_thresholds[0]]
    threshold_counts = [class_threshold_counts[class_id][t] for t in confidence_thresholds]
    threshold_percentages = [count / total_count * 100 if total_count > 0 else 0 for count in threshold_counts]

    print(f"{class_id:6d} | {total_count:5d} | ", end="")
    for percentage in threshold_percentages:
        print(f"{percentage:5.1f}% | ", end="")
    print()

print("-" * 100)
